#!/boot/apps/Squirrel/Squirrel.5.3
/*
 * Update!
 *
 * file ?	Update!
 * what	?	Squirrel updater
 * who	?	jlv
 * when	?	10/21/00
 * last	?	03/13/01
 *
 *
 * Kirilla 2000-2001
 */
 

/* used Add-Ons */

use 'GUI' 'String Processing' 'Threading' 'Storage' 'List Processing' 'Exec'

load :_install + '/Libraries/Colors.sqi' 

/* first things we check for the attribut "done" */

if (attr.exists :_file 'done') {
	
	if (Question "stop ['Oh yeah, that\'s true' 'I\'m not quiete sure ...'] '' 'This update had allready been applied') = 1 {
		bye
	}
		
}
 
/* global variables */

make "v.from 	'5.3b'
make "v.to		'5.3c'	
make "d.addon	:_path + '/Add-Ons'
make "d.distri	:_path + '/Distribution'
make "count		0
make "done 		false

make "how.many	(HowMany :d.addon) + (HowMany :d.distri)

/*
 * function    : HowMany
 * purpose     : Compute how many file we have to install
 * input       :
 *
 * path, path to count for
 *
 * output      : none
 * side effect : none
 */
to HowMany :path

	local "i
	make.local "items dir.list :path		
	make.local "count 0

	if (llength :items) {
		for.each "item :items {
			make "i :path + '/' + :item
			if (entry.isdir :i) {
				make "count :count + (HowMany :i)	
			} {
				make "count :count + 1	
			}		
		}
		
		output :count	
			
	} {
		output 0	
	}

end

/*
 * function    : Update
 * purpose     : Thread that update Squirrel distribution
 * input       : none
 * output      : none
 * side effect : none
 */
to Update

	; we going to process the 2 directories
	
	if (dir.exists :d.addon) {
		Replace :d.addon '/boot/home/config/add-ons/Squirrel'	
	}
		
	if (dir.exists :d.distri) {
		Replace :d.distri :_install	
	}

	$abort~enable false
	$quit~enable true
	make "done true
end 

/*
 * function    : Replace
 * purpose     : Replace all the item given in the source directory
 * input       :
 *
 * from, source directory
 * to, destination directory
 *
 * output      : none
 * side effect : none
 */
to Replace :from :to
	
	local "item "items	
	
	; we get the content of the directory
	
	make "items dir.list :from
	
	if (llength :items) {
		for.each "item :items {
			Copy :from :item :to
		}	
	}		
	
end

to Copy :path :item :to

	local "null

	make.local "i :path + '/' + :item

	; if we have a directory
	if (entry.isdir :i) {
		
		Replace	:i :to+'/'+:item
		
	} { ; we have a file
	
		make "count :count + 1	
		$status~update 1 :item	string :count	

		exec.wait "null '/bin/cp' :i :to
		exec.wait "null '/boot/beos/bin/copyattr' :i :to+'/'+:item		
	}	
	
end

; we init the font acess
Font.init

; we create a font
make "myfont Font
$myfont~size "set 14

; we create the window
make "win Window "titled 'Update!' [300 200] "not.closable "not.resizable

; we create a frame
make "frame Frame

; we create the label widget
make "label Text ('Squirrel ' + :v.from + ' to ' + :v.to)
$label~config "expand.x "set true
$label~justify "set "center
$label~config "font "set :myfont 
$label~config "high.color "set :Blue
$label~config "low.color "set :BeButtonGrey

; we create the statusbar
make "status StatusBar [20 2] :how.many 'file :' ('/' + (string :how.many))
$status~config "expand "set true true

; we create the button frame
make "fbut Frame
$fbut~config "expand "set true true
; and the buttons
make "update Button "Update
make "abort Button "Abort
$abort~enable false
make "quit Button "Quit

; we set the button hooks

Hook :update "invoked {

	; first we need to switch the button
	$update~enable false
	$abort~enable true
	$quit~enable false
		
	; then we start the thread
	make "th Thread "normal "Update	
	Thread.hop :th		
}

Hook :abort "invoked {
	
	Thread.kill :th
	$abort~enable false
	$quit~enable true	
		
}

Hook :quit "invoked {
	if :done {
		attr.set :_file 'done' :done
	}	
	$win~quit	
}

; we glue the button on the frame
Glue :fbut "left [] :update :abort :quit

; we glue the widget on the frame
Glue :frame "top [] :label :status :fbut

; we glue the frame to the window
Glue :win "top [] :frame

Focus :update

; and we show the window
$win~show




